﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using outREACH.App_Code;

namespace outREACH
{
    public partial class MedForumView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null)
            {
                Label lbl1 = (Label)Master.FindControl("Label1");
                lbl1.Text = "Welcome " + Session["Username"].ToString() + "!";
                Label lbl2 = (Label)Master.FindControl("Name");
                Users u = Users.GetName(Session["Username"].ToString());
                lbl2.Text = u.firstName + " " + u.lastName;
                Label lbl3 = (Label)Master.FindControl("Country");
                lbl3.Text = Session["country"].ToString();
                Button btn3 = (Button)Master.FindControl("Button4");
                if (User.IsInRole("admin"))
                {
                    btn3.Visible = true;
                }
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            MedForumReply mfr = new MedForumReply();
            int MF_ID = Convert.ToInt32(Request.QueryString["mid"]);
            string name = Session["userName"].ToString();
            string reply = TextBox66.Text;
            string category = "Medical";
            DateTime myDateTime = DateTime.Now;
            string path = "";
            string iname = "";
            string country = "";
            List<Users> results = Users.GetUser();
            foreach (Users u in results)
            {
                if (Session["Username"].ToString() == u.userName.ToString())
                {
                    path = u.imagePath.ToString();
                    iname = u.imageName.ToString();
                    country = u.country.ToString();
                }
            }
            string title = "";
            List<MedForum> result = MedForum.GetMedForum();
            foreach (MedForum mf in result)
            {
                if(Request.QueryString["mid"].ToString() == mf.MedForumID.ToString())
                {
                    title = mf.title.ToString();
                }
            }
            MedForumReply.insertMedReply(MF_ID, name, reply, myDateTime, category, path, iname, title, country);
            MedForum.updateMedReplyForum(MF_ID);
            MedForum.updateMedReplyForumDate(MF_ID, myDateTime);
            Annoucement.InsertAnnoucement("", "", "", myDateTime, "", name, reply, category, title.ToString(), "", 0, "", "");
            MedicalAnnoucement.InsertMedAnnoucement("", "", myDateTime, "", name, reply, title.ToString());
            Page.Response.Redirect(Page.Request.Url.ToString(), true);

        }
    }
}